import 'package:flutter/material.dart';

/**
 * Widget Image使用
 */
main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('基础Widge'),
      ),
      body: HYHomeContent(),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () {
            print('FloatingActionButton click');
          }),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}

class HYHomeContent extends StatefulWidget {
  @override
  _HYHomeContentState createState() => _HYHomeContentState();
}

class _HYHomeContentState extends State<HYHomeContent> {

  @override
  Widget build(BuildContext context) {
//    return ImageDemo();
    return ImageSimpleDemo();
  }
}

class ImageSimpleDemo extends StatelessWidget {
  final imageUrl =
      'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599419036385&di=57a8e30cd89d84e797eec9cef7d724ce&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F16%2F18%2F300000932954129238184537620_950.jpg';


  @override
  Widget build(BuildContext context) {
    return Image.asset(
      'assets/images/tupian.jpeg',
//    return Image.network(
//      imageUrl,
      //color: Colors.red,
      //colorBlendMode: BlendMode.colorDodge,
      width: 200,
      height: 400,
      //fitWidth 宽度一定，高度自适用
      fit: BoxFit.contain,
      //alignment: Alignment.bottomCenter,
      //范围： -1 1
      alignment: Alignment(0, 1),
      repeat: ImageRepeat.repeatY,
    );
  }
}

class ImageDemo extends StatelessWidget {
  const ImageDemo({
    Key key,
    @required this.imageUrl,
  }) : super(key: key);

  final String imageUrl;

  @override
  Widget build(BuildContext context) {
    return Image(
//      color: Colors.red,
//      colorBlendMode: BlendMode.colorDodge,
      //加载网络图片
//      image: NetworkImage(imageUrl),
      //加载本地图片
      //1.在flutter项目中创建一个文件夹，存储图片
      //2.在pubspec.yaml进行配置
      //3.使用图片
      image: AssetImage('assets/images/tupian.jpeg'),
      width: 200,
      height: 400,
      //fitWidth 宽度一定，高度自适用
      fit: BoxFit.contain,
//      alignment: Alignment.bottomCenter,
      //范围： -1 1
      alignment: Alignment(0, 1),
      repeat: ImageRepeat.repeatY,
    );
  }
}

class ImageExtensionDemo extends StatelessWidget {
  const ImageExtensionDemo({
    Key key,
    @required this.imageUrl,
  }) : super(key: key);

  final String imageUrl;

  @override
  Widget build(BuildContext context) {
    //1.占位图的问题: FadeInImage
    //2.图片缓存:1000张 100M
    return FadeInImage(
        fadeOutDuration: Duration(milliseconds: 1),
        fadeInDuration: Duration(milliseconds: 1),
        placeholder: AssetImage('assets/images/tupian.jpeg'),
        image: NetworkImage(imageUrl));
  }
}

